# -*- coding: utf-8 -*-


'''eof
name:个人被执行
code:IG_Enforce
tableName:
columnName:
groups:准入>个人通用
dependencies:EG
type:常用指标
dataSourceType:在线指标
description:
eof'''


import sys
import pandas as pd
import datetime
import re

reload(sys)
sys.setdefaultencoding('utf-8')

last_year_list = [(datetime.datetime.now() - pd.tseries.offsets.DateOffset(years=i)).strftime('%Y-%m-%d')\
                    for i in range(6)]

def IgEnforce():
    null_type_list = ['', None, 'null', 'Null', 'NULL', '/']
    count_list = []
    try:
        applicant_name = ratingRequest['legalPerson']
        applicant_id = ratingRequest['legalPersonIdCard']
        case = EG['srcData'][0]['EG_case']['data']
        companyName = EG['srcData'][0]['EG']['results'][0]['jbxx']['company_name']
        if len(case) >=1:
            for i in case:
                try:
                    if i.get('bbd_table') not in null_type_list:
                        if i['bbd_table'] == 'zhixing':
                            if i.has_key('pname_id') and i.has_key('idtype') \
                                    and i.has_key('pname') and (i['pname'] not in null_type_list
                                        or i['pname_id'] not in null_type_list or i['idtype'] not in null_type_list):
                                if (i.get('idtype') == u'身份证' or (i['pname'] == applicant_name and i['pname'] != companyName)
                                    or (i['pname_id'] not in null_type_list and re.compile(i['pname_id'].replace('*', '.*')).match(applicant_id) is not None )):
                                    if Formate(i.get('case_create_time')) is not None and Formate(i['case_create_time']) < last_year_list[5]:
                                        count_list.append(0)
                                    elif (Formate(i.get('case_create_time')) is None or ToFloat(i.get('exec_subject')) is None):
                                        count_list.append('None')
                                    elif Formate(i['case_create_time']) <= last_year_list[0] and Formate(i['case_create_time']) >= last_year_list[2]:
                                        if ToFloat(i['exec_subject']) <= 6000:
                                            count_list.append(0)
                                        else:
                                            count_list.append(1)
                                    elif Formate(i['case_create_time']) < last_year_list[2] and Formate(i['case_create_time']) >= last_year_list[3]:
                                        if ToFloat(i['exec_subject']) <= 15000:
                                            count_list.append(0)
                                        else:
                                            count_list.append(1)
                                    elif Formate(i['case_create_time']) < last_year_list[3] and Formate(i['case_create_time']) >= last_year_list[5]:
                                        if ToFloat(i['exec_subject']) <= 25000:
                                            count_list.append(0)
                                        else:
                                            count_list.append(1)
                                    else:
                                        count_list.append('None')
                                else:
                                    count_list.append(0)
                            else:
                                count_list.append(None)
                        else:
                            continue
                    else:
                        count_list.append(None)
                except:
                    count_list.append(None)
                    continue
            countNotNone = filter(lambda x: x is not None, count_list)
            countNotNoneNone = filter(lambda x: x != 'None', countNotNone)
            if len(countNotNone) == 0:
                return u'缺失值'
            elif sum(countNotNoneNone) > 0:
                return 1
            elif 'None' in countNotNone:
                return 2
            else:
                return 0
        else:
            return 0
    except:
        return u'缺失值'


def Formate(time):
    import re
    import datetime
    r = re.compile(r'\D+')
    try:
        date = datetime.datetime.strptime(time, '%Y.%m.%d').strftime('%Y-%m-%d')
    except:
        try:
            date = datetime.datetime.strptime(time, '%Y-%m-%d').strftime('%Y-%m-%d')
        except:
            try:
                date = datetime.datetime.strptime(time, '%Y/%m/%d').strftime('%Y-%m-%d')
            except:
                try:
                    tmp = r.sub('', time)
                    date = datetime.datetime.strptime(tmp, '%Y%m%d').strftime('%Y-%m-%d')
                except:
                    date = None
    return date


def ToFloat(value):
    try:
        return float(value) if float(value) > 0 else None
    except:
        return None


result = IgEnforce()